package response

type Pagination[T any] struct {
	List      []T   `json:"list"`
	Page      int   `json:"page"`
	Size      int   `json:"size"`
	Count     int64 `json:"count"`
	TotalPage int64 `json:"total_page"`
	HasPrev   bool  `json:"has_prev"`
	HasNext   bool  `json:"has_next"`
}

func (p Pagination[T]) Init(lst []T, page int, size int, count int64) *Pagination[T] {
	if page < 1 {
		page = 1
	}
	if size < 1 {
		size = 1
	}
	if count < 0 {
		count = 0
	}
	p.List = lst
	p.Size = size
	p.Page = page
	p.TotalPage = (count + int64(size) - 1) / int64(size)
	if int64(p.Page) > p.TotalPage {
		p.Page = int(p.TotalPage)
	}
	p.HasPrev = false
	p.HasNext = false
	if p.Page > 1 {
		p.HasPrev = true
	}
	if int64(p.Page) < p.TotalPage {
		p.HasNext = true
	}
	return &p
}
